package cn.iocoder.yudao.module.famous.doctor.service.inquiry;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.inquiry.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.inquiry.InquiryDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.famous.doctor.convert.inquiry.InquiryConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.inquiry.InquiryMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.famous.doctor.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 我的问诊 Service 实现类
 *
 * @author Test1
 */
@Service
@Validated
public class InquiryServiceImpl implements InquiryService {

    @Resource
    private InquiryMapper inquiryMapper;

    @Override
    public Long createInquiry(InquiryCreateReqVO createReqVO) {
        // 插入
        InquiryDO inquiry = InquiryConvert.INSTANCE.convert(createReqVO);
        inquiryMapper.insert(inquiry);
        // 返回
        return inquiry.getId();
    }

    @Override
    public void updateInquiry(InquiryUpdateReqVO updateReqVO) {
        // 校验存在
        validateInquiryExists(updateReqVO.getId());
        // 更新
        InquiryDO updateObj = InquiryConvert.INSTANCE.convert(updateReqVO);
        inquiryMapper.updateById(updateObj);
    }

    @Override
    public void deleteInquiry(Long id) {
        // 校验存在
        validateInquiryExists(id);
        // 删除
        inquiryMapper.deleteById(id);
    }

    private void validateInquiryExists(Long id) {
        if (inquiryMapper.selectById(id) == null) {
            throw exception(INQUIRY_NOT_EXISTS);
        }
    }

    @Override
    public InquiryDO getInquiry(Long id) {
        return inquiryMapper.selectById(id);
    }

    @Override
    public List<InquiryDO> getInquiryList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return inquiryMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<InquiryDO> getInquiryPage(InquiryPageReqVO pageReqVO) {
        return inquiryMapper.selectPage(pageReqVO);
    }
    @Override
    public PageResult<InquiryDO> getIoutpatientList(InquiryPageReqVO pageVO) {
        return inquiryMapper.getIoutpatientList(pageVO);
    }

    @Override
    public List<InquiryDO> getInquiryList(InquiryExportReqVO exportReqVO) {
        return inquiryMapper.selectList(exportReqVO);
    }

    @Override
    public InquiryDO getDiagnoseLabel(InquiryReqLabelVO exportReqVO) {
        return  inquiryMapper.selectOne(exportReqVO);
    }


}
